import csv
import requests
from bs4 import BeautifulSoup
start_url = 'http://books.toscrape.com'
with open('text.csv','w',newline='',encodint='utf-8') as f:
    writer = csv.writer(f)
    filetitle = ["书名", "图片", "价格", "评分", "库存", "产品介绍", "所属分类"]
    writer.writerow(filetitle)
    for j in range(2,51):
        r = requests.get(start_url)
        bs = BeautifulSoup(r.text, 'html.parser')
        for i in range(1,21):
            #书名
            bookname = bs.select("#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child({}) > article > h3 > a".format(i))[0].get('title')
            #书评分
            bookstore=bs.select("#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child({}) > article > p".format(i))[0].get('class')[1]
            #书价格
            bookprice=bs.select("#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child({}) > article > div.product_price > p.price_color".format(i))[0].text
            #请求书名内部数据
            title_in_url = bs.find('div', class_='image_container')
            in_url = 'http://books.toscrape.com/' + title_in_url.find('a')['href']
            r_in = requests.get(in_url)
            bs_in = BeautifulSoup(r_in.text, 'html.parser')
            #书图片
            bookpic=bs_in.find('div',class_='item active')
            bookpicture=bookpic.find('img').attrs['src']
            bookpicture =bookpicture.replace('../..','http://books.toscrape.com')
            #书库存
            bookquantity = bs_in.select("#content_inner > article > div.row > div.col-sm-6.product_main > p.instock.availability")[0].text
            bookquantity = bookquantity.replace('\n',"").replace(r' ',"").replace('In stock (',"").replace('available)',"").replace('Instock(',"")
            #产品介绍
            bookintroduce=bs_in.select("#content_inner > article > p")[0].text
            #所属分类
            bookclassify = bs_in.select("#default > div > div > ul > li:nth-child(3) > a")[0].text
            print(bookname)
            print(bookstore)
            print(bookprice)
            print(bookpicture)
            print(bookquantity)
            print(bookintroduce)
            print(bookclassify)
            fieldname = [bookname,bookpicture,bookprice,bookstore,bookquantity,bookintroduce,bookclassify]
            writer.writerow(fieldname)
    start_url='http://books.toscrape.com/catalogue/page-{}.html'.format(j)

